using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.ModelConfiguration;

namespace Daniel.Core.Models.Mapping
{
    public class CustomerMap : EntityTypeConfiguration<Customer>
    {
        public CustomerMap()
        {
            // Primary Key
            this.HasKey(t => t.Id);

            // Properties
            this.Property(t => t.Username)
                .HasMaxLength(1000);

            this.Property(t => t.Email)
                .HasMaxLength(1000);

            // Table & Column Mappings
            this.ToTable("Customer");
            this.Property(t => t.Id).HasColumnName("Id");
            this.Property(t => t.CustomerGuid).HasColumnName("CustomerGuid");
            this.Property(t => t.Username).HasColumnName("Username");
            this.Property(t => t.Email).HasColumnName("Email");
            this.Property(t => t.Password).HasColumnName("Password");
            this.Property(t => t.PasswordFormatId).HasColumnName("PasswordFormatId");
            this.Property(t => t.PasswordSalt).HasColumnName("PasswordSalt");
            this.Property(t => t.AdminComment).HasColumnName("AdminComment");
            this.Property(t => t.IsTaxExempt).HasColumnName("IsTaxExempt");
            this.Property(t => t.AffiliateId).HasColumnName("AffiliateId");
            this.Property(t => t.VendorId).HasColumnName("VendorId");
            this.Property(t => t.HasShoppingCartItems).HasColumnName("HasShoppingCartItems");
            this.Property(t => t.Active).HasColumnName("Active");
            this.Property(t => t.Deleted).HasColumnName("Deleted");
            this.Property(t => t.IsSystemAccount).HasColumnName("IsSystemAccount");
            this.Property(t => t.SystemName).HasColumnName("SystemName");
            this.Property(t => t.LastIpAddress).HasColumnName("LastIpAddress");
            this.Property(t => t.CreatedOnUtc).HasColumnName("CreatedOnUtc");
            this.Property(t => t.LastLoginDateUtc).HasColumnName("LastLoginDateUtc");
            this.Property(t => t.LastActivityDateUtc).HasColumnName("LastActivityDateUtc");
            this.Property(t => t.BillingAddress_Id).HasColumnName("BillingAddress_Id");
            this.Property(t => t.ShippingAddress_Id).HasColumnName("ShippingAddress_Id");

            // Relationships
            this.HasMany(t => t.CustomerRoles)
                .WithMany(t => t.Customers)
                .Map(m =>
                    {
                        m.ToTable("Customer_CustomerRole_Mapping");
                        m.MapLeftKey("Customer_Id");
                        m.MapRightKey("CustomerRole_Id");
                    });

            this.HasMany(t => t.Addresses)
                .WithMany(t => t.Customers2)
                .Map(m =>
                    {
                        m.ToTable("CustomerAddresses");
                        m.MapLeftKey("Customer_Id");
                        m.MapRightKey("Address_Id");
                    });

            this.HasOptional(t => t.Address)
                .WithMany(t => t.Customers)
                .HasForeignKey(d => d.BillingAddress_Id);
            this.HasOptional(t => t.Address1)
                .WithMany(t => t.Customers1)
                .HasForeignKey(d => d.ShippingAddress_Id);

        }
    }
}
